package com.example.demo.Service.Impl;

import com.example.demo.Service.UserService;
import com.example.demo.mapper.LibrarianMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Integer modifyUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        Integer result = userMapper.modifyUser(user);
        return result;
    }

    @Override
    public Integer addUser(User user){

        User t = userMapper.findUser(user.getUser_id());
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        if(t != null){
            return -1;
        }

        if(user.getUser_type() == null){
            user.setUser_type(0);
        }
        if(user.getUser_name() == null){
            user.setUser_name("未知用户");
        }
        Integer result = userMapper.addUser(user);
        return result;
    }

    @Override
    public List<User> getUserList(){
        List<User> result = userMapper.getUserList();
        return result;
    }

    @Override
    public User findUser(Integer id){
        User result = userMapper.findUser(id);
        return result;
    }

    @Override
    public Integer deleteUser(Integer id){
        Integer result = userMapper.deleteUser(id);
        return result;
    }
}
